# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=gd
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.2.5
pkgrel=3
pkgdesc='Library for the dynamic creation of images by programmers (mingw-w64)'
url='https://www.libgd.org/'
arch=('any')
license=("CPL")
options=(!libtool strip staticlibs)
depends=("${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libvpx"
         "${MINGW_PACKAGE_PREFIX}-xpm-nox")
#optdepends=('perl: bdftogd script')
source=("https://github.com/libgd/libgd/releases/download/gd-${pkgver}/libgd-${pkgver}.tar.xz"
        "php-gd-newvpx.patch")
sha256sums=('8c302ccbf467faec732f0741a859eef4ecae22fea2d2ab87467be940842bde51'
            '26409750a6d3deb7ced138daf5a37c514221f37281def372e542ffe3dde4c597')

prepare() {
  cd ${srcdir}/libgd-${pkgver}
}

build() {
  export lt_cv_deplibs_check_method='pass_all'
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}" && cd "${srcdir}/build-${MINGW_CHOST}"

  ../libgd-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --disable-static \
    --enable-shared \
    LIBXPM_CFLAGS="-I${MINGW_PREFIX}/include/noX" \
    LIBXPM_LIBS="-L${MINGW_PREFIX}/lib -lXpm"

  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make -j1 DESTDIR="${pkgdir}" install

  install -Dm644 ${srcdir}/libgd-${pkgver}/COPYING ${pkgdir}${MINGW_PREFIX}/share/licenses/libgd/LICENSE

  # Remove hard coded library path from pkg-config files
  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})

  pushd "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig" > /dev/null
  sed -s "s|${PREFIX_DEPS}\/lib|\${libdir}|g" -i ./gdlib.pc
  popd > /dev/null

  pushd "${pkgdir}/${MINGW_PREFIX}/bin" > /dev/null
  sed -s "s|${PREFIX_DEPS}\/lib|\${libdir}|g" -i gdlib-config
  popd
}
